﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Utilities;

namespace LOTSConversion
{
    public class ConversionConfig
    {
        private const string CONFIG_DIR = ".\\config\\";
        private const String CONFIG_FILE = "Conversion.config";

        //private const string REQUIRED_DIR = ".\\RequiredFiles\\";

        ////master database
        //public static string MASTER_DB_SERVER;
        //public static string MASTER_DB_NAME;
        //public static string MASTER_DB_USERNAME;
        //public static string MASTER_DB_PASSWORD;

        ////amfac helper database
        //public static string AMFAC_DB_SERVER;
        //public static string AMFAC_DB_NAME;
        //public static string AMFAC_DB_USERNAME;
        //public static string AMFAC_DB_PASSWORD;

        //conversion database
        public static string CONVERSION_DB_SERVER="";
        public static string CONVERSION_DB_NAME="";
        public static string CONVERSION_DB_USERNAME="";
        public static string CONVERSION_DB_PASSWORD="";

        public static string CONVERSION_DB_RECEIPTS_NAME="";


        //Fred HQ database
        //public static string FREDHQ_DB_SERVER;
        //public static string FREDHQ_DB_NAME;
        
        static ConversionConfig()
        {
            string configFileFolder = GetBaseDirectory() + CONFIG_DIR;
            string configFilePath = configFileFolder + CONFIG_FILE;

            XmlDocument doc = new XmlDocument();
            doc.Load(configFilePath);

            //try
            //{
            //    MASTER_DB_SERVER = doc["ConversionConfiguration"]["MasterDBConfig"]["ServerName"].Attributes["value"].Value;
            //    MASTER_DB_NAME = doc["ConversionConfiguration"]["MasterDBConfig"]["DBName"].Attributes["value"].Value;
            //    MASTER_DB_USERNAME = Encryption.DecryptData(doc["ConversionConfiguration"]["MasterDBConfig"]["DBUsername"].Attributes["value"].Value);
            //    MASTER_DB_PASSWORD = Encryption.DecryptData(doc["ConversionConfiguration"]["MasterDBConfig"]["DBPassword"].Attributes["value"].Value);
            //}
            //catch (Exception ex)
            //{
            //    throw new Exception("Master database connection not configured", ex);
            //}

                CONVERSION_DB_SERVER = doc["ConversionConfiguration"]["ConversionDBConfig"]["ServerName"].Attributes["value"].Value;
                CONVERSION_DB_NAME = doc["ConversionConfiguration"]["ConversionDBConfig"]["DBName"].Attributes["value"].Value;
                CONVERSION_DB_USERNAME = Encryption.DecryptData(doc["ConversionConfiguration"]["ConversionDBConfig"]["DBUsername"].Attributes["value"].Value);
                CONVERSION_DB_PASSWORD = Encryption.DecryptData(doc["ConversionConfiguration"]["ConversionDBConfig"]["DBPassword"].Attributes["value"].Value);

                CONVERSION_DB_RECEIPTS_NAME = CONVERSION_DB_NAME + "Receipts";

                //FREDHQ_DB_SERVER = doc["ConversionConfiguration"]["FredDBConfig"]["ServerName"].Attributes["value"].Value;
                //FREDHQ_DB_NAME = doc["ConversionConfiguration"]["FredDBConfig"]["DBName"].Attributes["value"].Value;

            //try
            //{
            //    AMFAC_DB_SERVER = doc["ConversionConfiguration"]["AmfacDBConfig"]["ServerName"].Attributes["value"].Value;
            //    AMFAC_DB_NAME = doc["ConversionConfiguration"]["AmfacDBConfig"]["DBName"].Attributes["value"].Value;
            //    AMFAC_DB_USERNAME = Encryption.DecryptData(doc["ConversionConfiguration"]["AmfacDBConfig"]["DBUsername"].Attributes["value"].Value);
            //    AMFAC_DB_PASSWORD = Encryption.DecryptData(doc["ConversionConfiguration"]["AmfacDBConfig"]["DBPassword"].Attributes["value"].Value);
            //}
            //catch (Exception ex)
            //{
            //    throw new Exception("AMFAC database connection not configured", ex);
            //}
        }
        
        private static String baseDirectory=null;
        protected static String GetBaseDirectory()
        {
            return (baseDirectory != null) ? baseDirectory : AppDomain.CurrentDomain.BaseDirectory;
        }

        //private static String requiredDirectory;
        //public static String GetRequiredDirectory()
        //{
        //    if (requiredDirectory == null)
        //        requiredDirectory = GetBaseDirectory() + REQUIRED_DIR;

        //    return requiredDirectory;
        //}

        //public static string GetMasterDatabaseConnectionString()
        //{
        //    return "Data Source=" + MASTER_DB_SERVER + ";Initial Catalog=" + MASTER_DB_NAME + ";User Id=" + MASTER_DB_USERNAME + ";Password=" + MASTER_DB_PASSWORD;
        //}



        public static string GetConversionDatabaseConnectionString()
        {
            return "Data Source=" + CONVERSION_DB_SERVER + ";Initial Catalog=" + CONVERSION_DB_NAME + ";User Id=" + CONVERSION_DB_USERNAME + ";Persist Security Info=True;Password=" + CONVERSION_DB_PASSWORD + ";Connect Timeout=0";
        }

        public static string GetConversionDatabaseReceiptsConnectionString()
        {
            return "Data Source=" + CONVERSION_DB_SERVER + ";Initial Catalog=" + CONVERSION_DB_RECEIPTS_NAME + ";User Id=" + CONVERSION_DB_USERNAME + ";Persist Security Info=True;Password=" + CONVERSION_DB_PASSWORD + ";Connect Timeout=0";
        }
        /*
        public static string GetFredHQDatabaseConnectionString()
        {
            //Server=myServerName\theInstanceName;Database=myDataBase;Trusted_Connection=True;

            //return "Data Source=" + CONVERSION_TEMP_DB_NAME + ";Initial Catalog=" + CONVERSION_DB_NAME + ";User Id=" + CONVERSION_DB_USERNAME + ";Password=" + CONVERSION_DB_PASSWORD;
            return "Data Source=" + FREDHQ_DB_SERVER + ";Initial Catalog=" + FREDHQ_DB_NAME + ";Integrated Security=True;Connect Timeout=0"; // ";User Id=" + CONVERSION_DB_USERNAME + ";Password=" + CONVERSION_DB_PASSWORD;
        }
        */
    }
}
